<?php

namespace app\controller\adminapi\diy;

use think\App;
use hiduoke\controller\BaseController;

/**
 * Notes: 系统DIY数据
 * Create by :  刘泓宾<514660094@qq.com>
 * Copyright :  copyright (c) 多客怪兽, www.dkyx.cc
 * Created on:  2025/8/6 03:30
 */
class Diy extends BaseController
{
    /**
     * @var \app\common\model\diy\Diy
     */
    protected $model;
    private array $where;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $key = input('key', '');
        $type = input('type', '');
        $plaid = input('plaid', 0);
        $this->where = ['type' => $type, 'plaid' => $plaid, 'key' => $key];
        $this->model = new \app\common\model\diy\Diy();
    }

    /**
     * 保存
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function save()
    {
        $diyData = input('diyData', []);
        $diy = $this->model->where($this->where)->findOrEmpty();
        $data = array(
            'value' => $diyData
        );
        $diy->save(array_merge($data, $this->where));
        return $this->success('保存成功', $data);
    }

    /**
     * 获取
     * Create by :  刘泓宾<514660094@qq.com>
     * @return \think\response\Json
     */
    public function get()
    {
        $diy = $this->model->where($this->where)->find();
        if (!$diy) {
            $value = [
                'rule'   => [],
                'config' => [],
            ];
        } else {
            $value = $diy['value'];
        }
        return $this->success('ok', $value);
    }
}
